package day20.pack1;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class Test2 {
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger ac = new AtomicInteger(); // CAS
        CountDownLatch countDownLatch = new CountDownLatch(4);
        Runnable r = () -> {
            for (int i = 0; i < 1000000; i++) {
                ac.getAndIncrement();
            }
            countDownLatch.countDown();
        };
        new Thread(r).start();
        new Thread(r).start();
        new Thread(r).start();
        new Thread(r).start();
        countDownLatch.await(); // 阻塞。到0
        System.out.println(ac.get());
    }
}
